#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
ll n, p, a, b;

bool check(ll x) {
    ll s = x * a + min((n + 6) / 7, 2 * x) * b;
    return s >= p;
}

void solve() {
    cin >> n >> p >> a >> b;
    ll l = 0, r = n;
    while (l < r) {
        ll mid = (l + r) >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << n - l << endl;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
